using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace HappySprite.Simulation
{
    public static class CircleHalfPlaneAlgorithm
    {
        public static bool DetectCollision(CollisionEventArgs e)
        {
            Circle circle = (Circle)e.This.CollisionShape;
            HalfPlane halfPlane = (HalfPlane)e.Other.CollisionShape;

            Vector2 p1 = e.This.Transform.Position;
            Vector2 p2 = e.Other.Transform.Position;

            // Uniform scaling only, get the largest scale factor
            float sf = Math.Max(e.This.Transform.Scale.X, e.This.Transform.Scale.Y);
            float radius = circle.Radius * sf;

            Vector2 normal = e.Other.Transform.TransformVector(-halfPlane.Normal);

            float depth = Vector2.Dot(normal, p1 - p2) + radius;

            if (depth < 0f)
                return false;

            if (e.CollisionDetails == CollisionDetails.None)
                return true;

            e.Normal = normal;
            e.Depth = depth;
            e.ContactPoint = p1 + radius * normal;

            return true;
        }
    }
}
